/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.milosnicy.miszcza.misiaela.mp3player.utils;

import java.awt.Frame;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.farng.mp3.AbstractMP3Tag;
import org.farng.mp3.id3.AbstractID3v2;
import org.farng.mp3.id3.ID3v1;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import pl.milosnicy.miszcza.misiaela.mp3player.model.MP3Track;
import pl.milosnicy.miszcza.misiaela.mp3player.view.MessageDialog;
import pl.milosnicy.miszcza.misiaela.mp3player.model.StringMessages;

/**
 *
 * @author Marcin.Wanke
 */
public class Mp3Utils {

	public static int getTrackDurationInSeconds(String filePath){
		int i = -1;
		try {
			File file = new File(filePath);
			MP3File f = (MP3File) AudioFileIO.read(file);
			MP3AudioHeader audioHeader = (MP3AudioHeader) f.getAudioHeader();
			i = audioHeader.getTrackLength();
		} catch (Exception ex) {}
		return i;
	}

	public static String getTrackInfo(String filePath){		
		File file = new File(filePath);
		AbstractMP3Tag tag = null;

		try {	
			org.farng.mp3.MP3File mp3File = new org.farng.mp3.MP3File(file);
			ID3v1 tag31 = mp3File.getID3v1Tag();
			AbstractID3v2 tag32 = mp3File.getID3v2Tag();			
			if(tag31 != null){
				tag = tag31;
			} else if(tag32 != null){
				tag = tag32;
			} else{
				System.out.println("unknown tag type");
			}
		} catch (Exception ex) {}	

		String performer = "";
		String title = "";
		String album = "";
		String year = "";
		String author = "";
		String genre = "";
		String trackNo = "";

		if(tag != null){
			try{
				performer = tag.getLeadArtist();
			} catch(Exception e){}

			try{
				title = tag.getSongTitle();
			} catch(Exception e){}

			try{
				album = tag.getAlbumTitle();
			} catch(Exception e){}

			try{
				year = tag.getYearReleased();
			} catch(Exception e){}

			try{
				author = tag.getAuthorComposer();
			} catch(Exception e){}

			try{
				genre = tag.getSongGenre();
			} catch(Exception e){}

			try{
				trackNo = tag.getTrackNumberOnAlbum();
			} catch(Exception e){}
		}

		String s = "<html>";
		s += StringMessages.PERFORMER + performer + "<br>";
		s += StringMessages.TITLE + title + "<br>";
		s += StringMessages.ALBUM + album + "<br>";
		s += StringMessages.YEAR_OF_RELEASE + year + "<br>";
		s += StringMessages.AUTHOR_COMPOSER + author + "<br>";
		s += StringMessages.SONG_GENRE + genre + "<br>";
		s += StringMessages.TRACK_NO_ON_ALBUM + trackNo + "<br>";
		s += "</html>";
		return s;
	}

	public static void showMessageDialog(Frame parent, String message){
		MessageDialog dlg = new MessageDialog(parent, message);
		dlg.setLocationRelativeTo(parent);
		dlg.setVisible(true);
	}

	public static String countMinutes(int seconds){
		int sec = seconds % 60;
		int min = seconds/60;
		return "" + ((min < 10)? "0" : "") + min + ":" + ((sec < 10)? "0" : "") + sec;
	}

	public static boolean containTrack(List<MP3Track> tracks, String trackName){
		for (MP3Track mP3Track : tracks) {
			if(mP3Track.getVisibleName().equals(trackName)){
				return true;
			}
		}
		return false;
	}

	public static List<MP3Track> grabAllTracksFromDir(File dir, boolean recursively){
		List<MP3Track> tracks = new ArrayList<MP3Track>();
		File[] filesAndDirs = dir.listFiles();
		for (File file : filesAndDirs) {
			if(file.isDirectory() && recursively){
				tracks.addAll(grabAllTracksFromDir(file, recursively));
			}

			if(file.isFile() && file.getName().toLowerCase().endsWith(".mp3")){
				tracks.add(new MP3Track(file.getAbsolutePath(), file.getName().substring(0, file.getName().length() - 4)));
			}
		}
		return tracks;
	}
}
